/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iffmyexp;

/**
 *
 * @author me
 */
import com.icesoft.faces.async.render.SessionRenderer;
import java.io.StringReader;
import java.util.Vector;
import javax.faces.event.ActionEvent;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class PushWorkflow {

    public static String workflow;
    public static String serverID;
    private String test = "test";
    public static String thumb = "/resources/css-images/connect_idle.gif";
    public static String desc = "";
    static inputModel[] inputs;
    public static int noOfInputs;
    public static int count;

    public PushWorkflow() {
        SessionRenderer.addCurrentSession("wfSession");
    }

    public String getWorkflow() {
        return workflow;
    }

    public static void setWorkflow(String w) {
        workflow = w;
        SessionRenderer.render("wfSession");
    }

    public String getServerID() {
        return serverID;
    }

    public void setServerID(String id) {
        this.serverID = id;
        SessionRenderer.render("wfSession");
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
        SessionRenderer.render("wfSession");
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
        SessionRenderer.render("wfSession");
    }

    public String getThumb() {
        return thumb;
    }

    public void setThumb(String thumb) {
        this.thumb = thumb;
        SessionRenderer.render("wfSession");
    }

    public void push(ActionEvent event) {

        search[] wfResArray = MyExperimentSearch.wfSearch;
        search wfRes = wfResArray[MyExperimentSearch.workflowsTable.getRowIndex()];
        workflow = wfRes.workflow;
        InterfaceGenerator.editNameVis = true;
        InterfaceGenerator.savedMessage = "";
        count = 0;

        String uri = wfRes.uri;


        try {
            DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = Factory.newDocumentBuilder();
            Document doc = builder.parse(uri + "&all_elements=yes");
            //creating an XPathFactory:
            XPathFactory factory = XPathFactory.newInstance();
            //using this factory to create an XPath object:
            XPath xpath = factory.newXPath();
            //XPath object created compiles the XPath expression for workflows:
            XPathExpression descExpr = xpath.compile("//description/text()");
            Object descResult = descExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList descNodeList = (NodeList) descResult;

            XPathExpression inputExpr = xpath.compile("//components/sources/source/name/text()");
            Object inputResult = inputExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList inputNodeList = (NodeList) inputResult;

            XPathExpression thumbExpr = xpath.compile("//thumbnail-big/text()");
            Object thumbResult = thumbExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList thumbNodeList = (NodeList) thumbResult;


            //set workflow variable values from xpath expressions
            desc = descNodeList.item(0).getNodeValue();
            if(desc.equals(""))
                desc = "No description available";

            inputs = new inputModel[inputNodeList.getLength()];
            for (int i = 0; i < inputNodeList.getLength(); i++) {
                inputModel inputName = new inputModel(inputNodeList.item(i).getNodeValue(), null, null, null, null, null, null, null, null,
                        "Interface component not defined", "/resources/icons/png/16x16/Warning.png", false);

                inputs[i] = inputName;
            }

            thumb = thumbNodeList.item(0).getNodeValue();

        } catch (Exception e) {
        }

        InterfaceGenerator.inputsTable.setValue(inputs);
        noOfInputs = inputs.length;
        //InterfaceGenerator.setConfirmVis(true);
        InterfaceGenerator.submitStatusText.setValue("");
        InterfaceGenerator.inputsVector = new Vector();
        //test = workflowDetails.getInputs()[0];
        //workflow = Page1.workflowsTable.getRowIndex();
        //thumb = workflowDetails.getThumb();
        SessionRenderer.render("wfSession");
    }

    public void createInterface(ActionEvent event) {
        //System.out.println("look");
        InterfaceGenerator.compsVis = true;
        thumb = "/resources/css-images/connect_active.gif";
        SessionRenderer.render("wfSession");
        push(event);
    }

    public static void pushServer(ActionEvent event) {

        InterfaceGenerator.editNameVis = true;
        InterfaceGenerator.compsVis = true;
        InterfaceGenerator.savedMessage = "";
        thumb = "/resources/css-images/connect_idle.gif";
        count = 0;
        desc = "";

        WorkflowDetails selectedWorkflow = ServerWorkflows.workflows[ServerWorkflows.workflowsTable.getRowIndex()];
        String WfId = selectedWorkflow.id;
        workflow = "Server workflow ID: " + WfId;
        serverID = WfId;

        try {
            DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
            Factory.setNamespaceAware(false);
            DocumentBuilder builder = Factory.newDocumentBuilder();
            Document doc = builder.parse("http://localhost:9090/taverna-server-0.2.1/rest/workflows/" + WfId);
            //creating an XPathFactory:
            XPathFactory factory = XPathFactory.newInstance();
            //using this factory to create an XPath object:
            XPath xpath = factory.newXPath();
            //XPath object created compiles the XPath expression for inputs:
            XPathExpression workflowXmlExpr = xpath.compile("//xml/text()");
            Object workflowXmlResult = workflowXmlExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList workflowXmlNodeList = (NodeList) workflowXmlResult;

            //System.out.println(workflowXmlNodeList.item(0).getNodeValue());

            //get input names
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(workflowXmlNodeList.item(0).getNodeValue()));

            Document wfXml = builder.parse(is);

            XPathExpression inputsExpr = xpath.compile("//dataflow[1]/inputPorts/port/name/text()");
            Object inputsResult = inputsExpr.evaluate(wfXml, XPathConstants.NODESET);
            NodeList inputsNodeList = (NodeList) inputsResult;

            System.out.println(inputsNodeList.item(0).getNodeValue());


            //set workflow variable values from xpath expressions
            //desc = descNodeList.item(0).getNodeValue();

            if (!inputsNodeList.equals(null)) {
                inputs = new inputModel[inputsNodeList.getLength()];
                for (int i = 0; i < inputsNodeList.getLength(); i++) {
                    inputModel inputName = new inputModel(inputsNodeList.item(i).getNodeValue(), null, null, null, null, null, null, null, null,
                            "Interface component not defined", "/resources/icons/png/16x16/Warning.png", false);

                    inputs[i] = inputName;
                }
            } else {
                inputs = new inputModel[1];
                inputModel in = new inputModel("no inputs", null, null, null, null, null, null, null, null,
                        "Interface component not defined", "/resources/icons/png/16x16/Warning.png", false);
                inputs[0] = in;
            }

        //thumb = thumbNodeList.item(0).getNodeValue();

        } catch (Exception e) {
        }

        InterfaceGenerator.inputsTable.setValue(inputs);
        noOfInputs = inputs.length;
        //InterfaceGenerator.setConfirmVis(true);
        InterfaceGenerator.submitStatusText.setValue("");
        InterfaceGenerator.inputsVector = new Vector();
        //test = workflowDetails.getInputs()[0];
        //workflow = Page1.workflowsTable.getRowIndex();
        //thumb = workflowDetails.getThumb();
        SessionRenderer.render("wfSession");
    }

    public static void pushServerUpload(ActionEvent event, Long wfid) {

        InterfaceGenerator.compsVis = true;
        InterfaceGenerator.editNameVis = true;
        InterfaceGenerator.savedMessage = "";
        thumb = "/resources/css-images/connect_idle.gif";
        count = 0;
        desc = "";

        //WorkflowDetails selectedWorkflow = ServerWorkflows.workflows[ServerWorkflows.workflowsTable.getRowIndex()];
        Long WfId = wfid;
        workflow = "Server workflow ID: " + WfId;
        serverID = Long.toString(WfId);

        try {
            DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
            Factory.setNamespaceAware(false);
            DocumentBuilder builder = Factory.newDocumentBuilder();
            Document doc = builder.parse("http://localhost:9090/taverna-server-0.2.1/rest/workflows/" + WfId);
            //creating an XPathFactory:
            XPathFactory factory = XPathFactory.newInstance();
            //using this factory to create an XPath object:
            XPath xpath = factory.newXPath();
            //XPath object created compiles the XPath expression for inputs:
            XPathExpression workflowXmlExpr = xpath.compile("//xml/text()");
            Object workflowXmlResult = workflowXmlExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList workflowXmlNodeList = (NodeList) workflowXmlResult;

            //System.out.println(workflowXmlNodeList.item(0).getNodeValue());

            //get input names
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(workflowXmlNodeList.item(0).getNodeValue()));

            Document wfXml = builder.parse(is);

            XPathExpression inputsExpr = xpath.compile("//dataflow[1]/inputPorts/port/name/text()");
            Object inputsResult = inputsExpr.evaluate(wfXml, XPathConstants.NODESET);
            NodeList inputsNodeList = (NodeList) inputsResult;

            System.out.println(inputsNodeList.item(0).getNodeValue());


            //set workflow variable values from xpath expressions
            //desc = descNodeList.item(0).getNodeValue();

            inputs = new inputModel[inputsNodeList.getLength()];
            for (int i = 0; i < inputsNodeList.getLength(); i++) {
                inputModel inputName = new inputModel(inputsNodeList.item(i).getNodeValue(), null, null, null, null, null, null, null, null,
                        "Interface component not defined", "/resources/icons/png/16x16/Warning.png", false);

                inputs[i] = inputName;
            }

        //thumb = thumbNodeList.item(0).getNodeValue();

        } catch (Exception e) {
        }

        InterfaceGenerator.inputsTable.setValue(inputs);
        noOfInputs = inputs.length;
        //InterfaceGenerator.setConfirmVis(true);
        InterfaceGenerator.submitStatusText.setValue("");
        InterfaceGenerator.inputsVector = new Vector();
        //test = workflowDetails.getInputs()[0];
        //workflow = Page1.workflowsTable.getRowIndex();
        //thumb = workflowDetails.getThumb();
        SessionRenderer.render("wfSession");
    }
}
